package com.sky.cqculture.service.impl;

import com.sky.cqculture.Mapper.UserMapper;
import com.sky.cqculture.pojo.User;
import com.sky.cqculture.service.UserService;
import com.sky.cqculture.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }
    
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }
    
    @Override
    public void register(User user) {
        // 检查手机号是否已被注册
        User existingUser = userMapper.findByPhone(user.getPhone());
        if (existingUser != null) {
            throw new RuntimeException("手机号已被注册");
        }
        
        // 密码加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        
        // 设置创建时间和更新时间
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        
        // 设置默认头像
        user.setAvatar("https://t7.baidu.com/it/u=3013355697,1688037142&fm=193&f=GIF");
        
        // 添加用户
        userMapper.insert(user);
    }
    
    @Override
    public Map<String, Object> login(String phone, String password) {
        // 根据手机号查询用户
        User user = userMapper.findByPhone(phone);
        if (user == null) {
            throw new RuntimeException("手机号未注册");
        }
        
        // 验证密码
        String encryptedPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!encryptedPassword.equals(user.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        
        // 生成JWT令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        claims.put("username", user.getUsername());
        String token = JwtUtils.getJwtToken(claims);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        
        // 构建用户信息，不包含密码等敏感信息
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("id", user.getId());
        userInfo.put("username", user.getUsername());
        userInfo.put("phone", user.getPhone());
        userInfo.put("avatar", user.getAvatar());
        result.put("userInfo", userInfo);
        
        return result;
    }
    
    @Override
    public void update(User user) {
        // 检查用户是否存在
        User existingUser = userMapper.findById(user.getId());
        if (existingUser == null) {
            throw new RuntimeException("用户不存在");
        }
        
        // 更新时间
        user.setUpdateTime(new Date());
        
        // 不允许更新密码，密码通过专门的方法修改
        user.setPassword(existingUser.getPassword());
        
        userMapper.update(user);
    }
    
    @Override
    public void changePassword(Integer id, String oldPassword, String newPassword) {
        // 检查用户是否存在
        User user = userMapper.findById(id);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        // 验证旧密码
        String encryptedOldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (!encryptedOldPassword.equals(user.getPassword())) {
            throw new RuntimeException("旧密码错误");
        }
        
        // 新密码加密
        String encryptedNewPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        
        // 更新密码
        userMapper.updatePassword(id, encryptedNewPassword);
        
        // 更新用户的更新时间
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }
    
    @Override
    public boolean verifyPassword(Integer userId, String password) {
        User user = userMapper.findById(userId);
        if (user == null) {
            return false;
        }
        
        String encryptedPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        return encryptedPassword.equals(user.getPassword());
    }
}